/*
 Privacy Friendly Finance Manager is licensed under the GPLv3.
 Copyright (C) 2019 Leonard Otto, Felix Hofmann

 This program is free software: you can redistribute it and/or modify it under the terms of the GNU
 General Public License as published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.
 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with this program.
 If not, see http://www.gnu.org/licenses/.

 Additionally icons from Google Design Material Icons are used that are licensed under Apache
 License Version 2.0.
 */

package org.secuso.privacyfriendlyfinance.domain.convert;

import androidx.room.*;

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

/**
 * Used to convert joda LocalDate to String and vise versa.
 *
 * @author Felix Hofmann
 * @author Leonard Otto
 */
public class LocalDateConverter {
    @TypeConverter
    public static LocalDate fromString(String value) {
        if (value == null) {
            return null;
        } else {
            return new LocalDate(value);
        }
    }

    @TypeConverter
    public static String dateToString(LocalDate date) {
        if (date == null) {
            return null;
        } else {
            DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
            return date.toString();
        }
    }
}
